package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.Exception.MyException;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;

import com.itheima.health.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public PageResult<Menu> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())) {
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }
        Page<Menu> menuPage = menuDao.findByCondition(queryPageBean.getQueryString());
        long total = menuPage.getTotal();
        List<Menu> rows = menuPage.getResult();
        return new PageResult<Menu>(total,rows);
    }

    @Override
    public void add(Menu menu) {
        if (!StringUtils.isEmpty(menu.getParentMenuId())) {
            Menu menu1 = menuDao.findByParentMenuId(menu.getParentMenuId());
            List<Menu> menuList = menuDao.findChildByParent(menu.getParentMenuId());
            int newLevel = menu1.getLevel() + 1;
            menu.setLevel(newLevel);
            int max = 0;
            if (menuList == null) {
                max = 1;
                menu.setPriority((max));
            }else{
                for (Menu menu2 : menuList) {
                    int priority = menu2.getPriority();
                    if(max < priority){
                        max = priority;
                    }
                }
                max = max + 1;
                menu.setPriority((max+1));
            }
            if (newLevel == 2) {
                menu.setPath("/"+menu1.getPath()+"-"+max);
            }else{
                menu.setPath(menu1.getPath()+"-"+max);
            }
            menuDao.add(menu);
        }else{
            menu.setLevel(1);
            List<Menu> menuList = menuDao.findParentMenu();
            int pr = 0;
            int pt = 0;
            if (menuList != null) {
                for (Menu menu1 : menuList) {
                    int priority = menu1.getPriority();
                    if(pr < priority){
                        pr = priority;
                    }
                    int path = Integer.parseInt(menu1.getPath());
                    if(pt < path){
                        pt = path;
                    }
                }
            }
            menu.setPath(String.valueOf(pt+1));
            menu.setPriority(pr+1);
            menuDao.add(menu);
        }
    }

    @Override
    public Menu findById(int id) {
        return menuDao.findById(id);
    }

    @Override
    public void update(Menu menu) {
        if (!StringUtils.isEmpty(menu.getParentMenuId())) {
            Menu menu1 = menuDao.findByParentMenuId(menu.getParentMenuId());
            List<Menu> menuList = menuDao.findChildByParent(menu.getParentMenuId());
            int newLevel = menu1.getLevel() + 1;
            menu.setLevel(newLevel);
            int max = 0;
            if (menuList == null) {
                max = 1;
                menu.setPriority((max));
            }else{
                for (Menu menu2 : menuList) {
                    int priority = menu2.getPriority();
                    if(max < priority){
                        max = priority;
                    }
                }
                max = max + 1;
                menu.setPriority((max+1));
            }
            if (newLevel == 2) {
                menu.setPath("/"+menu1.getPath()+"-"+max);
            }else{
                menu.setPath(menu1.getPath()+"-"+max);
            }
            menuDao.update(menu);
        }else{
            menu.setLevel(1);
            List<Menu> menuList = menuDao.findParentMenu();
            int pr = 0;
            int pt = 0;
            if (menuList != null) {
                for (Menu menu1 : menuList) {
                    int priority = menu1.getPriority();
                    if(pr < priority){
                        pr = priority;
                    }
                    int path = Integer.parseInt(menu1.getPath());
                    if(pt < path){
                        pt = path;
                    }
                }
            }
            menu.setPath(String.valueOf(pt+1));
            menu.setPriority(pr+1);
            menuDao.update(menu);
        }
    }

    @Override
    public void deleteById(int id) throws MyException {
        int count = menuDao.findCountByMenuId(id);

        if(count > 0){
            throw new MyException("该菜单项被角色使用了，不能删除");
        }

        int count1 = menuDao.findChildCountByParent(id);

        if(count1 > 0){
            throw new MyException("该菜单项有子菜单项，不能删除");
        }

        menuDao.deleteById(id);
    }
}
